var savestrp = [];
var sDateTraceRoute;
var eDateTraceRoute;
var i;
var FindCfmTraceRoute;
var SearchTraceRouteId;
var SearchTraceRouteArea;
var SearchTraceRouteDefine;
var savestr;
var childGridKey;
var flag;

$(document).ready(function() {
	var session = sessionStorage.getItem("session");
	if (!session) {
		window.parent.location = "../../login.html";
	}
	$('#sDateTraceRoute').datetimebox('setValue', ThreeDayBefore);
	
	$("input", $("#DeviceIdTraceRoute").next("span")).click(function() {
		$('#QueryTraceRouteTab').window('open');
	});
	
	SearchTraceRouteId = function() {
		var aDeviceid;
		savestrp = [""];
		flag = 1;
		aDeviceid = $("#QueryTraceRouteId").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (i = 0; i < aDeviceid.length; i++) {
			savestrp[i] = $("#QueryTraceRouteId").getCell(aDeviceid[i], 'deviceid');
		}
		console.log(savestrp);
		$("#DeviceIdTraceRoute").textbox('setValue', savestrp);
		$('#QueryTraceRouteTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}
	SearchTraceRouteArea = function() {
		var aArea;
		savestrp = [""];
		flag = 2;
		aArea = $("#QueryTraceRouteArea").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (i = 0; i < aArea.length; i++) {
			savestrp[i] = $("#QueryTraceRouteArea").getCell(aArea[i], 'text');
		}
		console.log(savestrp);
		$("#DeviceIdTraceRoute").textbox('setValue', savestrp);
		$('#QueryTraceRouteTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}
	SearchTraceRouteDefine = function() {
		var aDefine;
		savestrp = [""];
		flag = 3;
		aDefine = $("#QueryTraceRouteDefine").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (i = 0; i < aDefine.length; i++) {
			savestrp[i] = $("#QueryTraceRouteDefine").getCell(aDefine[i], 'probeGroup');
		}
		console.log(savestrp);
		$("#DeviceIdTraceRoute").textbox('setValue', savestrp);
		$('#QueryTraceRouteTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}

	$("#QueryTraceRouteGrid").jqGrid({
		colModel: [{
			label: '任务编号',
			name: 'taskid'
		}, {
			label: '设备编码',
			name: 'deviceid'
		}, {
			label: '序列号',
			name: 'seqnum'
		}, {
			label: '类型',
			name: 'type'
		}, {
			label: '执行时间',
			name: 'executeDate'
		}, {
			label: '执行耗时',
			name: 'executeTimespent'
		}, {
			label: '最大HotCount值',
			name: 'maxHopCount'
		}, {
			label: '包大小',
			name: 'packetSize'
		}, {
			label: 'IP地址',
			name: 'ipAddress'
		}, {
			label: 'HopCount值',
			name: 'hopCount'
		}],
		pagerpos: 'center',
		viewrecords: true,
		height: 480,
		rowNum: 17,
		autowidth: true,
		loadonce: true,
		page: 1,
		subGrid: true, // set the subGrid property to true to show expand buttons for each row
		subGridRowExpanded: showChildGrid, // javascript function that will take care of showing the child grid
		loadtext: '正在加载数据',
		pager: "#QueryTraceRouteGridPager",
		sortorder: "asc",
		sortname: 'id'
	});

	$(window).bind('resize', function() {
		$("#QueryTraceRouteGrid").setGridWidth($(window).width() * 1);
		$("#QueryTraceRouteGrid").setGridHeight($(window).height() - 110);
	});

	ExportFile = function() {
		$('#QueryTraceRouteGrid').jqGrid('setGridParam', {
			rowNum: 1000,
		}).trigger('reloadGrid');
		var title = "TcpPing测试报告";
		getXlsFromTbl('QueryTraceRouteGrid', 'gridtable', title, true);
		setTimeout(BackGrid(), 500);

		function BackGrid() {
			$('#QueryTraceRouteGrid').jqGrid('setGridParam', {
				rowNum: 17,
			}).trigger('reloadGrid');
		}
	}

	$('#QueryTraceRouteGrid').navGrid("#QueryTraceRouteGridPager", {
		search: false, // show search button on the toolbar
		add: false,
		edit: false,
		del: false,
		refresh: true
	});

	FindCfmTraceRoute = function() {
		sDateTraceRoute = $('#sDateTraceRoute').datetimebox('getValue').substr(0, 10);
		eDateTraceRoute = $('#eDateTraceRoute').datetimebox('getValue').substr(0, 10);
		if (DateRight(sDateTraceRoute, eDateTraceRoute)) {
			savestr = savestrp;
			if ((sDateTraceRoute != "") && (eDateTraceRoute != "") && (savestr != "")) {
				var myurl = IP + "QueryTraceRoute?callback=?&session=" + session + "&flag=" + flag + "&savestr=" + savestr + "&sDate=" + sDateTraceRoute + "&eDate=" + eDateTraceRoute;
				$('#QueryTraceRouteGrid').jqGrid('setGridParam', {
					url: myurl,
					type: 'Get',
					datatype: "jsonp",
				}).trigger('reloadGrid');
				console.log(myurl);
				insertopertlog('拨测报告', '路由追踪测试报告', '查询', '', '');
			} else {
				$.messager.show({
					title: '提示！',
					msg: '请选择设备信息',
					showType: 'fade',
					style: {
						right: '',
						bottom: ''
					}
				})
			}
		} else {
			$.messager.show({
				title: '提醒',
				msg: '请选择正确的时间范围！',
				timeout: 1300,
				showType: 'slide',
				style: {
					right: '',
					bottom: ''
				}
			});
		}
	}

	var filter;
	jQuery("#QueryTraceRouteId").jqGrid({
		url: IP + "GetProbeInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 175,
		width: 383,
		colNames: ['设备名称', '设备唯一编码'],
		colModel: [{
			name: 'deviceName',
			index: 'deviceName',
			sopt: ['eq'],
			width: 20
		}, {
			name: 'deviceid',
			index: 'deviceid',
			sopt: ['eq'],
			width: 40
		}],
		autowidth: false,
		shrinkToFit: true,
		//		shrinkToFit:false,
		loadonce: true,
		mtype: "GET",
		multiselect: true,
		multiselectWidth: 80,
		page: 1,
		viewrecords: true
	});
	var timer;
	$("#SearchCellTraceRouteId").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryTraceRouteId").jqGrid('filterInput', self.value);
		}, 0);
	});

	$("#QueryTraceRouteArea").jqGrid({
		url: IP + "GetAreaInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 175,
		width: 383,
		colModel: [{
			label: '县市',
			name: 'text',
			sopt: ['eq'],
			width: 125
		}],
		//		rowNum: 1000,		
		loadonce: true,
		mtype: "GET",
		multiselect: true,
		multiselectWidth: 80,
		gridview: true,
		page: 1,
		viewrecords: true
			//		sortorder: "asc",
			//		sortname: 'id'
	});
	$("#SearchCellTraceRouteArea").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryTraceRouteArea").jqGrid('filterInput', self.value);
		}, 0);
	});


	$("#QueryTraceRouteDefine").jqGrid({
		url: IP + "GetProbeGroupInfo?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '探针组',
			name: 'probeGroup',
			sopt: ['eq'],
			width: 50
		}, {
			label: '创建人',
			name: 'cPreson',
			sopt: ['eq'],
			width: 50
		}],
		loadonce: true,
		multiselect: true,
		multiselectWidth: 80,
		viewrecords: true,
		height: 175,
		width: 383,
		//			rowNum: 8,
		page: 1,
		loadtext: '正在加载数据',
	});
	$("#SearchCellTraceRouteDefine").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryTraceRouteDefine").jqGrid('filterInput', self.value);
		}, 0);
	});
	////--------------------------------------------tab按ID查询-----------------------------------------------------	
	function showChildGrid(parentRowID, parentRowKey) {
		var childGridID = parentRowID + "_table";
		var childGridPagerID = parentRowID + "_pager";
		childGridKey = $("#QueryTraceRoute").getCell(parentRowKey, 'taskid');
		// Rev the parent row primary key to the server so that we know which grid to show
		var childGridURL = IP + "QueryTraceRouteDetail?callback=?&session=" + session + "&taskid=" + childGridKey;
		console.log(childGridURL);
		//childGridURL = childGridURL + "&parentRowID=" + encodeURIComponent(parentRowKey)
		// add a table and pager HTML elements to the parent grid row - we will render the child grid here
		$('#' + parentRowID).append('<div style=' + "width:300px" + '><table id=' + childGridID + '></table><div id=' + childGridPagerID + ' class=scroll></div></div>');
		$("#" + childGridID).jqGrid({
			url: childGridURL,
			mtype: "Get",
			datatype: "jsonp",
			page: 1,
			colModel: [{
				label: '延时1',
				name: 'timeout1'
			}, {
				label: '延时2',
				name: 'timeout2'
			}, {
				label: '延时3',
				name: 'timeout3'
			}, {
				label: '序号',
				name: 'seq'
			}],
			loadonce: true,
			autowidth: true,
			rowNum:10,
			height: '100%',
			pager: "#" + childGridPagerID
		});
	}
})